#!/bin/bash

# 查找 ABBREVIATIONS 选项的位置
abbrev_line=$(grep -n "enable-abbreviations" ../configure.ac | head -1 | cut -d: -f1)

if [ -z "$abbrev_line" ]; then
    echo "错误：找不到 enable-abbreviations 选项"
    exit 1
fi

# 在 ABBREVIATIONS 选项前插入新的配置选项
sed -i "${abbrev_line}i\\
# ConfigManager plugins\\
AC_ARG_ENABLE([configmanager],\\
    [AS_HELP_STRING([--enable-configmanager],\\
        [build the configmanager plugin])],\\
    [enable_configmanager=\$enableval],\\
    [enable_configmanager=yes])\\
AC_ARG_ENABLE([configmanager-common],\\
    [AS_HELP_STRING([--enable-configmanager-common],\\
        [build the configmanager common library])],\\
    [enable_configmanager_common=\$enableval],\\
    [enable_configmanager_common=yes])\\
AC_ARG_ENABLE([configmanager-importers],\\
    [AS_HELP_STRING([--enable-configmanager-importers],\\
        [build the configmanager importers plugin])],\\
    [enable_configmanager_importers=\$enableval],\\
    [enable_configmanager_importers=yes])\\
AC_ARG_ENABLE([configmanager-exporters],\\
    [AS_HELP_STRING([--enable-configmanager-exporters],\\
        [build the configmanager exporters plugin])],\\
    [enable_configmanager_exporters=\$enableval],\\
    [enable_configmanager_exporters=yes])\\
" ../configure.ac

# 查找 AM_CONDITIONAL 的位置
conditional_line=$(grep -n "AM_CONDITIONAL.*ABBREVIATIONS" ../configure.ac | head -1 | cut -d: -f1)

if [ -n "$conditional_line" ]; then
    # 在 ABBREVIATIONS 条件前插入新的条件
    sed -i "${conditional_line}i\\
AM_CONDITIONAL([BUILD_CONFIGMANAGER], [test \"x\$enable_configmanager\" = xyes])\\
AM_CONDITIONAL([BUILD_CONFIGMANAGER_COMMON], [test \"x\$enable_configmanager_common\" = xyes])\\
AM_CONDITIONAL([BUILD_CONFIGMANAGER_IMPORTERS], [test \"x\$enable_configmanager_importers\" = xyes])\\
AM_CONDITIONAL([BUILD_CONFIGMANAGER_EXPORTERS], [test \"x\$enable_configmanager_exporters\" = xyes])\\
" ../configure.ac
fi

echo "configure.ac 更新完成"
